clear all
set more off , permanently
capture log close
set scheme lean2
set type double

******************************************************************************************************
********** This file analyzes the interaction of supply-side factors and the MPD effect **************
******************************************************************************************************

****************************************************
****   I Supply-side interactions: gasoline   ****** 
****************************************************
cap clear matrix
cap clear mata
eststo clear
	
	
// Load station characteristics

	* Identify non-integrated stations
		cd "$dta"
		use "stations.dta" , clear
		gen brand_new = "non-integrated"
		replace brand_new = "integrated" if brand_id == 1 | brand_id == 2 | brand_id == 3 | brand_id == 4 | brand_id == 6 | brand_id == 8 | brand_id == 9 | brand_id == 10 | brand_id == 11
		gen oligopolist = "no"
		replace oligopolist = "yes" if brand_id == 1 | brand_id == 2 | brand_id == 3 | brand_id == 4 | brand_id == 6
		keep id_data brand_new oligopolist
		rename id_data station_id
		tempfile stations_integration
		save `stations_integration' , replace
		
	* Load 5km markets Germany
		cd "$dta"
		use "station_markets_5.dta" , clear
		bysort master_id_data: gegen nr_comp_5km = count(tot_distance)
		replace nr_comp_5km = nr_comp_5km - 1
		keep master_id_data nr_comp_5km
		gcollapse(mean) nr_comp_5km , by(master_id_data)
		rename master_id_data station_id
		merge 1:m station_id using `stations_integration'
		drop if _merge == 1
		drop _merge
		cd "$dta"
		tempfile stations_characteristics
		save `stations_characteristics' , replace	
	
	
	
//  Baseline: 15 April 2013 - 31 March 2014 

	* Load price panel 
		cd "$dta"
		use germany_france.dta , clear

	* Keep only for restricted analysis period
		keep if date > d(12apr2013) & date < d(31aug2014)

	* Drop dates after the introduction of the MTU with a temporary (1-3 day) drop of notifications of more than 40%
		drop if date == d(29nov2013) | date == d(10dec2013) | date == d(22dec2013) | date == d(01jan2014) | date == d(29aug2014) | date == d(01sep2014)
		drop if date >= d(08nov2014) & date <= d(11nov2014)
			
	* Generate treatment variable
		gen treatment_group = 0
		replace treatment_group = 1 if country == "de"
		gen treatment_period = 0
		replace treatment_period = 1 if date >= date("20130912","YMD")
		
	* Run DiD for prices
		drop if e5_margin_17oclock == .

	* Define treatment variable
		gen treated = 0
		replace treated = 1 if treatment_group == 1 & treatment_period == 1
		
	* Replace all prices between 12 September 2013 and 01 October 2013 as missing
		replace e5_17oclock_panel = . if date > d(12sep2013) & date < d(01oct2013)

* Keep only necessary variables for station level pass-through estimation
	keep station_id date e5_17oclock_panel country treated
		
* Set panel structure
	egen id=group(station_id)
	xtset id date

* Gen log price
	gen log_price = log(e5_17oclock_panel)
	drop e5_17oclock_panel

* Merge station characteristics in Germany to the data set
	merge m:1 station_id using `stations_characteristics'
	drop if _merge == 2
	drop _merge

* Generate treatment interactions with number of competitors
	g tr_comp_1 = 0
	replace tr_comp_1 = treated if nr_comp_5km == 1
	g tr_comp_2 = 0
	replace tr_comp_2 = treated if nr_comp_5km == 2
	g tr_comp_3 = 0
	replace tr_comp_3 = treated if nr_comp_5km == 3
	g tr_comp_4 = 0
	replace tr_comp_4 = treated if nr_comp_5km == 4
	g tr_comp_5more = 0
	replace tr_comp_5more = treated if nr_comp_5km >= 5
	
* Generate treatment interactions with integrated / oligopolist
	g tr_oligopoly = 0
	replace tr_oligopoly = treated if oligopolist == "yes"
	g tr_integrated = 0
	replace tr_integrated = treated if brand_new == "integrated"
		
eststo gas_oli: reghdfe log_price treated tr_oligopoly , ab(station_id date) cl(station_id)
	
eststo gas_int: reghdfe log_price treated tr_integrated , ab(station_id date) cl(station_id)
	
eststo gas_nrcomp: reghdfe log_price treated tr_comp_1 tr_comp_2 tr_comp_3 tr_comp_4 tr_comp_5more , ab(station_id date) cl(station_id)




	
	
**************************************************
****   I Supply-side interactions: diesel   ****** 
**************************************************	
	
// Load station characteristics

	* Identify non-integrated stations
		cd "$dta"
		use "stations.dta" , clear
		gen brand_new = "non-integrated"
		replace brand_new = "integrated" if brand_id == 1 | brand_id == 2 | brand_id == 3 | brand_id == 4 | brand_id == 6 | brand_id == 8 | brand_id == 9 | brand_id == 10 | brand_id == 11
		gen oligopolist = "no"
		replace oligopolist = "yes" if brand_id == 1 | brand_id == 2 | brand_id == 3 | brand_id == 4 | brand_id == 6
		keep id_data brand_new oligopolist
		rename id_data station_id
		tempfile stations_integration
		save `stations_integration' , replace
		
	* Load 5km markets Germany
		cd "$dta"
		use "station_markets_5.dta" , clear
		bysort master_id_data: gegen nr_comp_5km = count(tot_distance)
		replace nr_comp_5km = nr_comp_5km - 1
		keep master_id_data nr_comp_5km
		gcollapse(mean) nr_comp_5km , by(master_id_data)
		rename master_id_data station_id
		merge 1:m station_id using `stations_integration'
		drop if _merge == 1
		drop _merge
		cd "$dta"
		tempfile stations_characteristics
		save `stations_characteristics' , replace	
	
	
	
//  Baseline: 15 April 2013 - 31 March 2014 

	* Load price panel 
		cd "$dta"
		use germany_france.dta , clear

	* Keep only for restricted analysis period
		keep if date > d(12apr2013) & date < d(31aug2014)

	* Drop dates after the introduction of the MTU with a temporary (1-3 day) drop of notifications of more than 40%
		drop if date == d(29nov2013) | date == d(10dec2013) | date == d(22dec2013) | date == d(01jan2014) | date == d(29aug2014) | date == d(01sep2014)
		drop if date >= d(08nov2014) & date <= d(11nov2014)
			
	* Generate treatment variable
		gen treatment_group = 0
		replace treatment_group = 1 if country == "de"
		gen treatment_period = 0
		replace treatment_period = 1 if date >= date("20130912","YMD")
		
	* Run DiD for prices
		drop if gazole_margin_17oclock == .

	* Define treatment variable
		gen treated = 0
		replace treated = 1 if treatment_group == 1 & treatment_period == 1
		
	* Replace all prices between 12 September 2013 and 01 October 2013 as missing
		replace gazole_17oclock_panel = . if date > d(12sep2013) & date < d(01oct2013)

* Keep only necessary variables for station level pass-through estimation
	keep station_id date gazole_17oclock_panel country treated
		
* Set panel structure
	egen id=group(station_id)
	xtset id date

* Gen log price
	gen log_price = log(gazole_17oclock_panel)
	drop gazole_17oclock_panel

* Merge station characteristics in Germany to the data set
	merge m:1 station_id using `stations_characteristics'
	drop if _merge == 2
	drop _merge

* Generate treatment interactions with number of competitors
	g tr_comp_1 = 0
	replace tr_comp_1 = treated if nr_comp_5km == 1
	g tr_comp_2 = 0
	replace tr_comp_2 = treated if nr_comp_5km == 2
	g tr_comp_3 = 0
	replace tr_comp_3 = treated if nr_comp_5km == 3
	g tr_comp_4 = 0
	replace tr_comp_4 = treated if nr_comp_5km == 4
	g tr_comp_5more = 0
	replace tr_comp_5more = treated if nr_comp_5km >= 5
	
* Generate treatment interactions with integrated / oligopolist
	g tr_oligopoly = 0
	replace tr_oligopoly = treated if oligopolist == "yes"
	g tr_integrated = 0
	replace tr_integrated = treated if brand_new == "integrated"
	
		
eststo diesel_oli: reghdfe log_price treated tr_oligopoly , ab(station_id date) cl(station_id)
	
eststo diesel_int: reghdfe log_price treated tr_integrated , ab(station_id date) cl(station_id)

eststo diesel_nrcomp: reghdfe log_price treated tr_comp_1 tr_comp_2 tr_comp_3 tr_comp_4 tr_comp_5more , ab(station_id date) cl(station_id)
	
			
	// Gen Table 4: Supply-side factors mediating MPD effect
		cd "$output"
		esttab gas_nrcomp diesel_nrcomp gas_int diesel_int gas_oli diesel_oli using "Table 4_supplyside factors.tex" , b(3) se(4) ar2 replace booktabs noconstant star(* 0.10 ** 0.05 *** 0.01) label ///
			title(Effect of MPD under different supply-side conditions\label{tab_tr: supply-side}) ///
			nonumbers mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)") ///
			addnote("SEs clustered at the fuel station level.")
		
